import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from "@/stores/user";
import CertificationResult from '@/views/search/components/CertificationResult.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/yindao'
    },
    {
      path: '/yindao',
      name: 'yindao',
      component: () => import('../views/yindao/index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue')
    },
    {
      path: '/regist',
      name: 'regist',
      component: () => import('../views/regist/index.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/search/index.vue')
    },
    {
      path: '/chatAI',
      name: 'chatAI',
      component: () => import('../views/chatAI/index.vue')
    },
    {
      path: '/mine',
      name: 'mine',
      component: () => import('../views/mine/index.vue')
    },
    {
      path: '/modifyPassword',
      name: 'modifyPassword',
      component: () => import('../views/mine/conponents/ModifyPassword.vue')
    },
    {
      path: '/ChangeAvatar',
      name: '修改头像',
      component: () => import('../views/mine/conponents/ChangeAvatar.vue')
    },
    {
      path: '/MyInfo',
      name: '个人界面',
      component: () => import('@/views/home/components/MyInfo.vue')
    },
    {
      path: '/tweetLike',
      name: '文章收藏',
      component: () => import('@/views/home/components/tweetLike.vue')
    },
    {
      path: '/articleHistory',
      name: '文章浏览历史',
      component: () => import('@/views/home/components/articleHistory.vue')
    },
    {
      path: '/EditInfo',
      name: '修改个人信息',
      component: () => import('@/views/home/components/EditInfo.vue')
    },
    {
      path: '/doctorInformation',
      name: 'doctorInformation',
      meta: { title: '科室详情' },
      component: () => import('@/components/doctorConsultation/doctorInformation.vue'),
    },
    {
      path: '/docPerInfo',
      name: 'docPerInfo',
      meta: { title: '医生个人详情页' },
      component: () => import('@/components/doctorConsultation/docInfoPage.vue'),
    },
    {
      path: '/onlineAsk',
      name: 'onlineAsk',
      meta: { title: '医生问诊' },
      component: () => import('@/components/doctorConsultation/components/onlineAsk.vue'),
    },
    {
      path: '/petHospitalMap',
      name: 'petHospitalMap',
      meta: { title: '宠物医院地图' },
      component: () => import('@/views/home/components/petHospitalMap.vue'),
    },
    {
      path: '/certification-result',
      name: 'CertificationResult',
      component: CertificationResult,
    },
    {
      path: '/article/:id',
      name: 'ArticleContent',
      component: () => import('../views/home/components/articleContent.vue')
    },
    {
      path: '/petStore',
      name: 'PetStoreIndex',
      component: () => import('../views/petStore/index.vue')
    },
    {
      path: '/petStore/list/:id/:name',
      name: 'ProductList',
      component: () => import('../views/productList/index.vue'),
      props: true
    },
    {
      path: '/petStore/product:id',
      name: 'ProductDetail',
      component: () => import('../views/productDetail/index.vue')
    },
    {
      path: '/petStore/shoppingCart/:id',
      name: 'ShoppingCart',
      component: () => import('../views/petStore/shoppingCart/shoppingCart.vue')
    },
    {
      path:'/order',
      name: 'Order',
      component:() => import('../views/order/index.vue'),
      props: (route) => ({
        orderInfo: route.query.orderInfo ? JSON.parse(route.query.orderInfo) : null
      })
    },
    {
      path: '/order/cart',
      name: 'CartOrder',
      component: () => import('@/views/order/components/cart.vue')
    },
    {
      path: '/order/myOrders/:id',
      name: 'MyOrders',
      component: () => import('@/views/order/components/myOrders.vue')
    },
    {
      path: '/order/detail/:id',
      name: 'OrderDetail',
      component: () => import('@/views/order/components/orderDetail.vue')
    },
    {
      path: '/order/payment',
      name: 'OrderPayment',
      component: () => import('@/views/order/components/payment.vue')
    },
    {
      path: '/address',
      name: 'Address',
      component: () => import('@/views/address/index.vue')
    },
    {
      path: '/community',
      name: 'Community',
      component: () => import('../views/community/index.vue')
    },
    {
      path: '/import',
      name: 'Import',
      component: () => import('../views/community/components/import.vue')

    },
    {
      path: '/protdetail/:id',
      name: 'ProtDetail',
      component: () => import('../views/community/components/protcontext.vue')
    },
    {
      path: '/articleCollection',
      name: 'articleCollection',
      component: () => import('../views/home/components/articleCollection.vue')
    },

  ]
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();

  // 判断是否需要登录权限
  if (to.meta.requiresAuth) {
    // 检查是否有 token
    if (!userStore.token) {
      // 没有 token，跳转到登录页
      next('/login');
    } else {
      // 有 token，允许访问
      next();
    }
  } else {
    next();
  }
});

export default router
